package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.JournalDutyMapper;
import com.zhijian.medical.entity.po.JournalDuty;
import com.zhijian.medical.entity.vo.request.JournalDutySearchVo;
import com.zhijian.medical.entity.vo.request.JournalDutySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.JournalDutyVo;
import com.zhijian.medical.service.JournalDutyService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 期刊任职业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-05-11 11:16:15
 */
@Slf4j
@Service
public class JournalDutyServiceImpl implements JournalDutyService {

	@Autowired
    private JournalDutyMapper mapper;

    @Override
    public JournalDuty selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public JournalDutyVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(JournalDuty journalDuty) {
        Date now = new Date();
        journalDuty.setCreateTime(now);
        journalDuty.setUpdateTime(now);
        return mapper.insert(journalDuty) > 0 ? journalDuty.getId() : 0L;
    }

    @Override
    public boolean update(JournalDuty journalDuty) {
        Date now = new Date();
        journalDuty.setUpdateTime(now);
        return mapper.updateByPrimaryKey(journalDuty) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(JournalDuty record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(JournalDutySaveOrUpdateVo saveOrUpdateVo) {
        JournalDuty journalDuty = new JournalDuty();
        BeanUtils.copyProperties(saveOrUpdateVo, journalDuty);
        return this.insert(journalDuty) != 0;
    }

    @Override
    public boolean modify(JournalDutySaveOrUpdateVo saveOrUpdateVo) {
        JournalDuty old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        JournalDuty journalDuty = new JournalDuty();
        BeanUtils.copyProperties(saveOrUpdateVo, journalDuty);
        return this.update(journalDuty);
    }

    @Override
    public JournalDutyVo selectOne(JournalDutySearchVo journalDutySearchVo) {
        return mapper.selectOne(journalDutySearchVo);
     }

    @Override
    public Integer count(JournalDutySearchVo journalDutySearchVo) {
        return mapper.count(journalDutySearchVo);
    }

    @Override
    public List<JournalDutyVo> selectList(JournalDutySearchVo journalDutySearchVo) {
        return mapper.selectList(journalDutySearchVo);
    }

    @Override
    public Page<JournalDutyVo> selectPage(JournalDutySearchVo journalDutySearchVo) {
        AssertUtil.check(Objects.isNull(journalDutySearchVo.getPageNum()) || Objects.isNull(journalDutySearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(journalDutySearchVo);
        //分页
        Page<JournalDutyVo> page = new Page<>(true, count, journalDutySearchVo.getPageNum(), journalDutySearchVo.getPageSize());
        journalDutySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<JournalDutyVo> responseVos = this.selectList(journalDutySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<JournalDuty> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(JournalDutySearchVo journalDutySearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(journalDutySearchVo);
    }
    
}